package com.gzc.just.play.last.war.common.share.persistence;

import com.gzc.just.play.last.war.common.share.cache.CacheableRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Optional;

/**
 * 鍩虹鏁版嵁璁块棶鎺ュ彛
 * 鎻愪緵閫氱敤鐨勬暟鎹闂搷浣?
 */
@NoRepositoryBean
public interface BaseRepository<T, ID extends Serializable> extends JpaRepository<T, ID>, CacheableRepository<T, ID> {
    
    /**
     * 鎵归噺鏌ヨ骞剁紦瀛?
     */
    @Transactional(readOnly = true)
    List<T> findAllByOrderByIdAsc();
    
    /**
     * 鏍规嵁鏉′欢鍒嗛〉鏌ヨ
     */
    @Transactional(readOnly = true)
    Page<T> findByCondition(String condition, Pageable pageable);
    
    /**
     * 鎵归噺鎻掑叆
     */
    @Transactional
    List<T> batchInsert(List<T> entities);
    
    /**
     * 鎵归噺鏇存柊
     */
    @Transactional
    List<T> batchUpdate(List<T> entities);
    
    /**
     * 杞垹闄わ紙濡傛灉瀹炰綋鏀寔锛?
     */
    @Transactional
    int softDeleteById(ID id);
    
    /**
     * 杞垹闄わ紙濡傛灉瀹炰綋鏀寔锛?
     */
    @Transactional
    int softDeleteByIds(Iterable<ID> ids);
    
    /**
     * 鏍规嵁鏉′欢鏌ヨ
     */
    @Transactional(readOnly = true)
    @Query("select e from #{#entityName} e where 1=1 and ?1")
    List<T> findByCustomCondition(String condition);
    
    /**
     * 缁熻鎬绘暟
     */
    @Transactional(readOnly = true)
    @Query("select count(e) from #{#entityName} e")
    long countAll();
}
